package com.ds.dsm.view.config.menu.service;

import com.ds.common.JDSException;
import com.ds.common.util.StringUtility;
import com.ds.config.ErrorListResultModel;
import com.ds.config.ErrorResultModel;
import com.ds.config.ResultModel;
import com.ds.config.TreeListResultModel;
import com.ds.dsm.view.config.menu.FormMenuPopTree;
import com.ds.esd.custom.annotation.DialogAnnotation;
import com.ds.esd.custom.annotation.PopTreeViewAnnotation;
import com.ds.esd.custom.api.annotation.APIEventAnnotation;
import com.ds.esd.custom.api.enums.CustomCallBack;
import com.ds.esd.custom.bean.MethodConfig;
import com.ds.esd.custom.bean.nav.NavBaseViewBean;
import com.ds.esd.custom.enums.CustomMenuItem;
import com.ds.esd.custom.form.CustomFormViewBean;
import com.ds.esd.custom.form.enums.CustomFormMenu;
import com.ds.esd.custom.grid.enums.GridMenu;
import com.ds.esd.custom.module.annotation.ModuleAnnotation;
import com.ds.esd.dsm.DSMFactory;
import com.ds.esd.dsm.view.ViewEntityConfig;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@RequestMapping(path = "/dsm/view/config/form/")
public class FormMenuService {


    @PopTreeViewAnnotation
    @ModuleAnnotation(dynLoad = true, width = "300", height = "450", caption = "表单按钮")
    @RequestMapping("FormMenuTree")
    @DialogAnnotation
    @APIEventAnnotation(bindMenu = CustomMenuItem.add)
    @ResponseBody
    public TreeListResultModel<List<FormMenuPopTree>> getMenuPopTree(String sourceClassName, String methodName, String viewInstId, String esdsearchpattern) {
        TreeListResultModel<List<FormMenuPopTree>> model = new TreeListResultModel<>();
        List<FormMenuPopTree> popTrees = new ArrayList<>();
        try {
            FormMenuPopTree menuPopTree = new FormMenuPopTree("formMenu", "常用按钮", "FormMenuTree");
            for (CustomFormMenu menu : CustomFormMenu.values()) {
                FormMenuPopTree item = new FormMenuPopTree(esdsearchpattern, menu);
                item.addTagVar("sourceClassName", sourceClassName);
                item.addTagVar("viewInstId", viewInstId);
                item.addTagVar("methodName", methodName);
                menuPopTree.addChild(item);
            }
            popTrees.add(menuPopTree);
        } catch (JDSException e) {
            model = new ErrorListResultModel();
            ((ErrorListResultModel) model).setErrdes(e.getMessage());
        }
        model.setData(popTrees);
        return model;
    }

    @PopTreeViewAnnotation
    @ModuleAnnotation(dynLoad = true, width = "300", height = "450", caption = "表单按钮")
    @RequestMapping("BottomMenuTree")
    @DialogAnnotation
    @APIEventAnnotation(bindMenu = CustomMenuItem.add)
    @ResponseBody
    public TreeListResultModel<List<FormMenuPopTree>> getMenuBottomTree(String sourceClassName, String methodName, String viewInstId, String esdsearchpattern) {
        TreeListResultModel<List<FormMenuPopTree>> model = new TreeListResultModel<>();
        List<FormMenuPopTree> popTrees = new ArrayList<>();
        try {
            FormMenuPopTree menuPopTree = new FormMenuPopTree("bottombar", "表单按钮", "FormBottomTree");
            for (CustomFormMenu menu : CustomFormMenu.values()) {
                FormMenuPopTree item = new FormMenuPopTree(esdsearchpattern, menu);
                item.addTagVar("sourceClassName", sourceClassName);
                item.addTagVar("viewInstId", viewInstId);
                item.addTagVar("methodName", methodName);
                menuPopTree.addChild(item);
            }
            popTrees.add(menuPopTree);
        } catch (JDSException e) {
            model = new ErrorListResultModel();
            ((ErrorListResultModel) model).setErrdes(e.getMessage());
        }
        model.setData(popTrees);
        return model;
    }

    @PopTreeViewAnnotation
    @ModuleAnnotation(dynLoad = true, width = "300", height = "450", caption = "表单按钮")
    @RequestMapping("ToolBarsTree")
    @DialogAnnotation
    @APIEventAnnotation(bindMenu = CustomMenuItem.add)
    @ResponseBody
    public TreeListResultModel<List<FormMenuPopTree>> getToolBarsTree(String sourceClassName, String methodName, String viewInstId, String esdsearchpattern) {
        TreeListResultModel<List<FormMenuPopTree>> model = new TreeListResultModel<>();
        List<FormMenuPopTree> popTrees = new ArrayList<>();
        try {
            FormMenuPopTree menuPopTree = new FormMenuPopTree("toolbar", "表单按钮", "ToolBarsTree");
            for (CustomFormMenu menu : CustomFormMenu.values()) {
                FormMenuPopTree item = new FormMenuPopTree(esdsearchpattern, menu);
                item.addTagVar("sourceClassName", sourceClassName);
                item.addTagVar("viewInstId", viewInstId);
                item.addTagVar("methodName", methodName);
                menuPopTree.addChild(item);
            }
            popTrees.add(menuPopTree);
        } catch (JDSException e) {
            model = new ErrorListResultModel();
            ((ErrorListResultModel) model).setErrdes(e.getMessage());
        }
        model.setData(popTrees);
        return model;
    }


    @RequestMapping("addMenu")
    @APIEventAnnotation(bindMenu = CustomMenuItem.treeSave, callback = {CustomCallBack.ReloadParent, CustomCallBack.Close})
    @ResponseBody
    public ResultModel<Boolean> addMenu(String sourceClassName, String methodName, String FormMenuTree, String BottomMenuTree, String ToolBarsTree, String domainId, String viewInstId) {

        ResultModel<Boolean> model = new ResultModel<>();
        try {
            ViewEntityConfig tableConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName,  viewInstId);
            MethodConfig methodAPIBean = tableConfig.getSourceConfig().getMethodByName(methodName);

            if (methodAPIBean.getView() instanceof NavBaseViewBean) {
                NavBaseViewBean viewBean = (NavBaseViewBean) methodAPIBean.getView();
                if (FormMenuTree != null && !FormMenuTree.equals("")) {
                    String[] menuIds = StringUtility.split(FormMenuTree, ";");
                    for (String menuId : menuIds) {
                        if (menuId != null && !menuIds.equals("")) {
                            viewBean.getCustomMenu().add(CustomFormMenu.valueOf(menuId));
                        }
                    }

                } else if (BottomMenuTree != null && !BottomMenuTree.equals("")) {
                    String[] menuIds = StringUtility.split(BottomMenuTree, ";");
                    for (String menuId : menuIds) {
                        if (menuId != null && !menuIds.equals("")) {
                            viewBean.getBottombarMenu().add(GridMenu.valueOf(menuId));
                        }
                    }
                }

            } else if (methodAPIBean.getView() instanceof CustomFormViewBean) {

                CustomFormViewBean formViewBean = (CustomFormViewBean) methodAPIBean.getView();
                if (FormMenuTree != null && !FormMenuTree.equals("")) {
                    String[] menuIds = StringUtility.split(FormMenuTree, ";");
                    for (String menuId : menuIds) {
                        if (menuId != null && !menuIds.equals("")) {
                            formViewBean.getCustomMenu().add(CustomFormMenu.valueOf(menuId));
                        }
                    }

                } else if (BottomMenuTree != null && !BottomMenuTree.equals("")) {
                    String[] menuIds = StringUtility.split(BottomMenuTree, ";");
                    for (String menuId : menuIds) {
                        if (menuId != null && !menuIds.equals("")) {
                            formViewBean.getBottombarMenu().add(CustomFormMenu.valueOf(menuId));
                        }
                    }

                } else if (ToolBarsTree != null && !ToolBarsTree.equals("")) {
                    String[] menuIds = StringUtility.split(ToolBarsTree, ";");
                    for (String menuId : menuIds) {
                        if (menuId != null && !menuIds.equals("")) {
                            formViewBean.getToolBarMenu().add(CustomFormMenu.valueOf(menuId));
                        }
                    }
                }
            }


            DSMFactory.getInstance().getViewManager().updateViewEntityConfig(tableConfig);
        } catch (
                JDSException e)

        {
            model = new ErrorResultModel();
            ((ErrorResultModel<Boolean>) model).setErrdes(e.getMessage());
        }


        return model;
    }


    @RequestMapping("delMenu")
    @APIEventAnnotation(bindMenu = CustomMenuItem.delete, callback = {CustomCallBack.Reload})
    @ResponseBody
    public ResultModel<Boolean> delMenu(String sourceClassName, String methodName, String type, String bar, String domainId, String viewInstId) {
        ResultModel<Boolean> model = new ResultModel<>();
        try {
            ViewEntityConfig tableConfig = DSMFactory.getInstance().getViewManager().getViewEntityConfig(sourceClassName, viewInstId);
            MethodConfig methodAPIBean = tableConfig.getSourceConfig().getMethodByName(methodName);
            if (methodAPIBean.getView() instanceof NavBaseViewBean) {
                NavBaseViewBean viewBean = (NavBaseViewBean) methodAPIBean.getView();
                String[] menuIds = StringUtility.split(type, ";");
                for (String menuId : menuIds) {
                    viewBean.getCustomMenu().remove(CustomFormMenu.valueOf(menuId));
                    viewBean.getBottombarMenu().remove(CustomFormMenu.valueOf(menuId));
                }
            } else if (methodAPIBean.getView() instanceof CustomFormViewBean) {
                CustomFormViewBean formViewBean = (CustomFormViewBean) methodAPIBean.getView();
                String[] menuIds = StringUtility.split(type, ";");
                for (String menuId : menuIds) {
                    formViewBean.getCustomMenu().remove(CustomFormMenu.valueOf(menuId));
                    formViewBean.getBottombarMenu().remove(CustomFormMenu.valueOf(menuId));
                }
            }
            DSMFactory.getInstance().getViewManager().updateViewEntityConfig(tableConfig);
        } catch (JDSException e) {
            model = new ErrorResultModel();
            ((ErrorResultModel<Boolean>) model).setErrdes(e.getMessage());
        }
        return model;
    }


}
